package com.guigu.pd.base.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.base.entity.GoodsTypeEntity;
import com.guigu.pd.base.entity.TruckDriverEntity;
import com.guigu.pd.base.entity.TruckEntity;
import com.guigu.pd.base.service.TruckDriverService;
import com.guigu.pd.base.service.TruckService;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.base.dao.FleetDao;
import com.guigu.pd.base.entity.FleetEntity;
import com.guigu.pd.base.service.FleetService;


@Service("fleetService")
public class FleetServiceImpl extends ServiceImpl<FleetDao, FleetEntity> implements FleetService {


    @Autowired
    private TruckService truckService;

    @Autowired
    private TruckDriverService truckDriverService;

    @Autowired
    private CoreOrgFeign coreOrgFeign;


    @Override
    public PageVo<FleetEntity> queryfleettypefy(Map<String, Object> params) {


//        QueryWrapper<FleetEntity> queryWrapper = new QueryWrapper<FleetEntity>();
//        if (StringUtils.isNotBlank(fleetEntity.getName())) {
//            queryWrapper.like("name", fleetEntity.getName());
//        }
//        if (StringUtils.isNotBlank(fleetEntity.getFleetNumber())) {
//            queryWrapper.like("fleet_number", fleetEntity.getFleetNumber());
//        }

        FleetEntity
                fleetEntity = JSON.parseObject(JSON.toJSONString(params),
                FleetEntity.class);

        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        LambdaQueryWrapper<FleetEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(fleetEntity.getName()),FleetEntity::getName,fleetEntity.getName());
        queryWrapper.like(StringUtils.isNotEmpty(fleetEntity.getFleetNumber()),FleetEntity::getFleetNumber,fleetEntity.getFleetNumber());

        PageHelper.startPage(pageno, pagesize);
        List<FleetEntity> list = this.list(queryWrapper);

        for (FleetEntity entity : list) {

            QueryWrapper<TruckEntity> queryWrapper2 = new QueryWrapper<TruckEntity>();

            queryWrapper2.eq("fleet_id",entity.getId());
            //查询该车队下面的车辆数量
            int count = (int) truckService.count(queryWrapper2);
            entity.setTrucknumber(count);

            QueryWrapper<TruckDriverEntity> queryWrapper3 = new QueryWrapper<TruckDriverEntity>();

            queryWrapper2.eq("fleet_id",entity.getId());
            //查询该车队下面的司机数量
            int count1 = (int) truckDriverService.count(queryWrapper3);
            entity.setDrivernumber(count1);

            //查询该车队所属机构名称
            String getcornamebyid = coreOrgFeign.getcornamebyid(entity.getAgencyId());
            entity.setAgencyname(getcornamebyid);

        }
        PageInfo<FleetEntity> pageInfo = new PageInfo<FleetEntity>(list);

        PageVo<FleetEntity> pageVo = new PageVo<FleetEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }
}